package icasue.runninginvoke.api;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import icasue.runninginvoke.invoke.RunningInvoking;
import icasue.runninginvoke.exposebucket.RunningInvokeExposePointBucket;


/**
 * @Author: Qiao Hang
 * @CreateDate: 2020/9/10 下午1:36
 * @UpdateDate:
 * @Description:
 */
@RestController
@RequestMapping("/running-invoke")
public class RunningInvokeAPI {

    @RequestMapping(value = "/infos",method = RequestMethod.GET)
    public Object infos(){
        JSONObject infos  = new JSONObject();
        infos.put("运行时调度API",getCommonInvokeInfo());
        infos.put("获取应用程序暴露点API",getExposePointInfo());
        return infos;
    }

    @RequestMapping(value = "/commonInvoke",method = RequestMethod.POST)
    public Object commonInvoke(@RequestBody RunningInvoking.InvokerBucket invokerBucket,
                               @RequestHeader(name = "waitingResult", required = false) Boolean waitingResult){
        Object invokerResult = RunningInvoking.commonInvoke(invokerBucket,waitingResult);
        return invokerResult;
    }

    @RequestMapping(value = "/getExposePoint",method = RequestMethod.GET)
    public Object getExposePoint(){
        return RunningInvokeExposePointBucket.exposePointBucket;
    }

    private JSONObject getCommonInvokeInfo(){
        JSONObject commonInvokeInfo = new JSONObject();
        commonInvokeInfo.put("API","{context-path}/running-invoke/commonInvoke");
        commonInvokeInfo.put("请求方式","P0ST");
        commonInvokeInfo.put("请求结果","目标方法的调度结果");
        commonInvokeInfo.put("API描述","提供类似Spring Junit的调度，细粒度:方法级，返回方法调度结果.");
        commonInvokeInfo.put("请求格式","{\n" +
                "    \"functionDescription\": \"功能描述，可空\",\n" +
                "    \"className\": \"目标调度全类名(注意内部类写法)\",\n" +
                "    \"methodName\": \"目标调度方法\",\n" +
                "    \"params\": 参数表列\n" +
                "  }");
        commonInvokeInfo.put("模拟请求","{\n" +
                "    \"functionDescription\": \"Mock句柄请求\",\n" +
                "    \"className\": \"com.icasue.controller.MockController\",\n" +
                "    \"methodName\": \"mockMethodName\",\n" +
                "    \"params\": [\n" +
                "      {\n" +
                "        \"paramDoc\": \"门店ID\",\n" +
                "        \"paramClassName\": \"java.lang.String\",\n" +
                "        \"paramValue\": null\n" +
                "      },\n" +
                "      {\n" +
                "        \"paramDoc\": \"\",\n" +
                "        \"paramClassName\": \"java.lang.String\",\n" +
                "        \"paramValue\": null\n" +
                "      }\n" +
                "    ]\n" +
                "  }");
        return commonInvokeInfo;
    }

    private JSONObject getExposePointInfo(){
        JSONObject exposePointInfo = new JSONObject();
        exposePointInfo.put("API","{context-path}/running-invoke/getExposePoint");
        exposePointInfo.put("请求方式","GET");
        exposePointInfo.put("请求结果","当前应用程序扫描到的暴露点");
        exposePointInfo.put("API描述","获取应用程序反射暴露点");
        exposePointInfo.put("请求格式","");
        exposePointInfo.put("模拟请求","");
        return exposePointInfo;
    }

}
